package mts.testframework.tests;


import java.io.FileWriter;
import java.io.IOException;

import mts.ElevatorControllerInternalConnectionServer;
import mts.ElevatorControllerManager;
import mts.constants.Direction;
import mts.groupdispatcher.FloorRequest;
import mts.groupdispatcher.GroupDispatcher;
import mts.testframework.ScheduleCompletedListener;
import mts.testframework.TestItemScheduler;
import mts.testframework.testitems.FloorRequestTestItem;
import mts.testframework.testitems.HallCallTestItem;

/**
 * @author Nick
 *
 * Morning Rush: Apartment Building
 */
public class MorningRushStressTest implements ScheduleCompletedListener {

	private TestItemScheduler scheduler;
	private int numOfCars;
	
	public void startTest(GroupDispatcher groupDispatcher, ElevatorControllerInternalConnectionServer internalConnectionServer, int numOfCars) {
		this.numOfCars = numOfCars;
		ElevatorControllerManager.stopAllElevatorControllerProcesses();
		
		for (byte i = 0; i < numOfCars; i++) {
			ElevatorControllerManager.startElevatorProcess((byte)i);
		}
		
		
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		scheduler = new TestItemScheduler(groupDispatcher, internalConnectionServer);
		
		scheduler.schedule(
				new FloorRequestTestItem(0 , new FloorRequest((byte)8, Direction.DOWN), 
						new HallCallTestItem(0, (byte)0, (byte)1)),
				new FloorRequestTestItem(0, new FloorRequest((byte)6, Direction.DOWN),
						new HallCallTestItem(0, (byte)0, (byte)1)),
				new FloorRequestTestItem(0, new FloorRequest((byte)5, Direction.DOWN),
						new HallCallTestItem(0, (byte)0, (byte)1)),
				
				// TODO 9 * 2 seconds + door time eg. 9*2 + 5
				new FloorRequestTestItem((9 * 2 + 5) *1000, new FloorRequest((byte)9, Direction.DOWN)),
				new FloorRequestTestItem((9 * 2 + 5+2) *1000, new FloorRequest((byte)2, Direction.DOWN))
				
				);
		scheduler.addOnCompletedListener(this);
		
	
	}

	@Override
	public void scheduleCompleted() {
		try {
			FileWriter fstream = new FileWriter("./stress tests/MorningRushStresTest" + numOfCars + ".txt");
			fstream.write(scheduler.getOutput());
			fstream.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
